<template>
<div class="login-container">
    <van-nav-bar title="登录" class="page-nav-bar" >
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <van-form  @submit="onSubmit" ref="formRef">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="user.mobile"
        :rules="userRules.mobile"
      >
        <!-- <i class="toutiao toutiao-shouji" slot="left-icon"></i> -->
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        :rules="userRules.code"
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
      >
        <i class="toutiao toutiao-yanzhengma" slot="left-icon"></i>
        <template #button>
          <van-count-down v-if="isCountDownShow" @finish="isCountDownShow=false" :time="5000" format="sss" />
          <van-button v-else size="mini" round class="yzm" native-type="button" @click="onSendSms">发送验证码</van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button  block type="info" native-type="submit" class="login-btn">登录</van-button>
      </div>
    </van-form>
</div>
</template>

<script>
import { login, getSmsCode } from '@/api/user'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      isCountDownShow: false,
      user: {
        mobile: '', // 手机号
        code: ''// 验证码
      },
      userRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          // \d 匹配数字
          // [3|5|7|8] 或
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onSendSms () {
      // 校验手机号格式
      try {
        await this.$refs.formRef.validate('mobile')
      } catch (err) {
        return console.log('校验失败')
      }
      this.isCountDownShow = true
      console.log('校验成功')
      try {
        await getSmsCode(this.user.mobile)
        this.$toast.success('发送成功')
      } catch (err) {
        // 发送失败，关闭倒计时
        this.isCountDownShow = false
        console.dir(err)
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    },
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const res = await login(this.user)
        console.log(res)
        this.$toast.success('登录成功')
        this.$store.commit('setUser', res.data.data)
        this.$router.back()
      } catch (err) {
        console.dir(err)
        if (err.response.status === 400) {
          console.log('手机号或验证码错误')
          this.$toast.fail('手机号或验证码错误')
        } else {
          console.log('其他错误')
          this.$toast.fail('其他错误')
        }
      }
    }
  }
}
</script>

<style scoped>
.toutiao{
  font-size:40px;
}
.yzm{
  background-color: #ededed;
}
.login-btn{
  background-color: #6db4fb;
  border-color:#6db4fb;
}
</style>
